var numi = 1;
let pages = 0;
let page = 0;
var fenyearr = "";
var classs = JSON.parse(localStorage.getItem("classs"));
var employees = JSON.parse(localStorage.getItem("employees"));
var classtype = JSON.parse(localStorage.getItem("classtype"));


$(document).ready(function(){
    initialize(classs);
})
$(".sear").click(function (e) { 
    e.preventDefault();
    let str = $(".sear_tiao").val();
    searDep(str);
});
$(".bu").change(function (e) { 
    e.preventDefault();
    let str = $(".bu").val();
    searDep1(str);
});
$("#addq").click(function (e) { 
    e.preventDefault();
    let classname = $(".caddname").val();
    let classtype = $(".bu1").val();
    let classt1 = $(".caddban1").val();
    let classt2 = $(".caddban2").val();

    if(classname == "" || classtype == "") {

    }else {
        addDep(classname,classtype,classt1,classt2,classs);
    }
    $("#depAdd").modal('hide');
    $(".caddname").val("");
    $(".bu1").val("");
    $(".caddban1").val("");
    $(".caddban1").val("");
});
$("#delq").click(function (e) { 
    e.preventDefault();
    classs.splice(numi,1);
    localStorage.setItem("classs", JSON.stringify(classs));
    classs = JSON.parse(localStorage.getItem("classs"));
    $("#depDel").modal('hide');
    initia(classs);
});
$("#upq").click(function (e) { 
    e.preventDefault();
    console.log(classs[numi]);
    classs[numi].classname = $(".upname").val();
    classs[numi].classtype = $(".bu1").val();
    classs[numi].teacher1 = $(".ban1").val();
    classs[numi].teacher2 = $(".ban2").val();
    $("#depUp").modal('hide');
    $(".caddname").val("");
    $(".bu1").val("");
    $(".ban1").val("");
    $(".ban2").val("");
    localStorage.setItem("classs", JSON.stringify(classs));
    initia(classs);
});
$("#ti").click(function (e) { 
    e.preventDefault();
    console.log(classs[numi].typeid);
    if(classs[numi].typeid <= classtype.length) {
        classs[numi].typeid += 1;
        for(let i = 0;i < classtype.length; i++) {
            if (classs[numi].typeid == classtype[i].id) {
                classs[numi].classtype = classtype[i].typename;
                break;
            }
        }
    }
    localStorage.setItem("classs", JSON.stringify(classs));
    $("#depTi").modal('hide');
    initia(classs);
});
$(".juzhong a").click(function (e) { 
    e.preventDefault();
    num = $(this).parent().val();
    if(page != 1) {
        if(num==98) {
            page--;
            initia(fenyearr);
        }
    }
    if(page != pages) {
        if(num==99) {
            page++
            initia(fenyearr);
        }
    }
});
$(".ban1").change(function (e) { 
    e.preventDefault();
    $(".ban2").empty();
    for (let i = 0; i < employees.length; i++) {
        if($(".ban1").val() == employees[i].Name) {
            continue;
        }else {
            if(employees[i].DepID == 1) {
                let str = `<option>${employees[i].Name}</option>`;
                $(".ban2").append(str);
            }
        }
    }
});

$('#depAdd').on('show.bs.modal', function (event) {
    $(".bu1").empty();
    $(".ban1").empty();
    $(".ban2").empty();

    for (let i = 0; i < classtype.length; i++) {
        let str = `<option>${classtype[i].typename}</option>`;
        $(".bu1").append(str);
    }
    for (let i = 0; i < employees.length; i++) {
        if(employees[i].DepID == 1) {
            let str = `<option>${employees[i].Name}</option>`;
            $(".ban1").append(str);
            $(".ban2").append(str);
        }
    }
})
$('#depDel').on('show.bs.modal', function (event) {
    let num = $($(event.relatedTarget).parent().parent().siblings()).html();
    var result = classs.findIndex((v) => {
        return v.classnum === num;
    });
    numi = result;
    $(".cdelnum").html(classs[result].classnum);
    $(".cdelname").html(classs[result].classname);
})
$('#depUp').on('show.bs.modal', function (event) {
    let num = $($(event.relatedTarget).parent().parent().siblings()).html();
    var result = classs.findIndex((v) => {
        return v.classnum === num;
    });
    numi = result;
    $(".upname").attr("placeholder",classs[result].classname);
    $(".bu1").empty();
    $(".ban1").empty();
    $(".ban2").empty();

    for (let i = 0; i < classtype.length; i++) {
        let str = "";
        if(classs[result].classtype == classtype[i].typename) {
            str = `<option selected >${classtype[i].typename}</option>`;
        }else {
            str = `<option>${classtype[i].typename}</option>`; 
        }
        $(".bu1").append(str);
    }
    for (let i = 0; i < employees.length; i++) {
        let str = "";
        if(employees[i].DepID == 1) {
            if(classs[result].teacher1 == employees[i].Name) {
                str = `<option selected>${employees[i].Name}</option>`;
            }else {
                str = `<option>${employees[i].Name}</option>`;
            }
            $(".ban1").append(str);
            if(classs[result].teacher2 == employees[i].Name) {
                str = `<option selected>${employees[i].Name}</option>`;
            }else {
                str = `<option>${employees[i].Name}</option>`;
            }
            $(".ban2").append(str);
        }
    }
})
$('#depShow').on('show.bs.modal', function (event) {
    let num = $($(event.relatedTarget).parent().parent().siblings()).html();
    var result = classs.findIndex((v) => {
        return v.classnum === num;
    });
    // numi = num; 
    // console.log(Department[result].DepName);
    $(".showcnum").html(classs[result].classnum);
    $(".showcname").html(classs[result].classname);
    $(".showt1").html(classs[result].teacher1);
    $(".showt2").html(classs[result].teacher2);

})
$('#depTi').on('show.bs.modal', function (event) {
    let num = $($(event.relatedTarget).parent().parent().siblings()).html();
    var result = classs.findIndex((v) => {
        return v.classnum === num;
    });
    numi = result;
    $(".tiname").html(classs[result].classname);
    $(".tigrade").html(classs[result].classtype);
})



//初始化加载
function initialize(arr1) {
    fenyearr = arr1;
    let arr = fenye(arr1);
    for (let i = 0; i < classtype.length; i++) {
        let str = `<option>${classtype[i].typename}</option>`;
        $(".bu").append(str);
    }
   
    for(let i = 0; i<arr.length; i++) {
        
        let str1 = "";
        if(arr[i].typeid == 4) {
            str1 = `<tr>
                        <td>${arr[i].classnum}</td>
                        <td>${arr[i].classname}</td>
                        <td>${arr[i].classtype}</td>
                        <td>
                            <div class="btn-group" role="group" aria-label="...">
                                <button type="button" class="btn btn-default" data-toggle="modal" data-target="#depDel" id="del1">删除</button>
                                <button type="button" class="btn btn-default" data-toggle="modal" data-target="#depUp" id="cup">修改</button>
                                <button type="button" class="btn btn-default" data-toggle="modal" data-target="#depShow" id="ck">查看</button>
                            </div>
                        </td>
                    </tr>`;
        }else {
            str1 = `<tr>
                        <td>${arr[i].classnum}</td>
                        <td>${arr[i].classname}</td>
                        <td>${arr[i].classtype}</td>
                        <td>
                            <div class="btn-group" role="group" aria-label="...">
                                <button type="button" class="btn btn-default" data-toggle="modal" data-target="#depDel" id="del1">删除</button>
                                <button type="button" class="btn btn-default" data-toggle="modal" data-target="#depUp" id="cup">修改</button>
                                <button type="button" class="btn btn-default" data-toggle="modal" data-target="#depShow" id="ck">查看</button>
                                <button type="button" class="btn btn-default" data-toggle="modal" data-target="#depTi" id="ti">提升</button>
                            </div>
                        </td>
                    </tr>`;
        }
        $(".dep_table").append(str1);
    }
}
//二次加载
function initia(arr1) {
    fenyearr = arr1;
    $(".biaotou").siblings().remove();
    let arr = fenye(arr1);
    for(let i = 0; i<arr.length; i++) {
        let str1 = "";
        if(arr[i].typeid == 4) {
            str1 = `<tr>
                        <td>${arr[i].classnum}</td>
                        <td>${arr[i].classname}</td>
                        <td>${arr[i].classtype}</td>
                        <td>
                            <div class="btn-group" role="group" aria-label="...">
                                <button type="button" class="btn btn-default" data-toggle="modal" data-target="#depDel" id="del1">删除</button>
                                <button type="button" class="btn btn-default" data-toggle="modal" data-target="#depUp" id="cup">修改</button>
                                <button type="button" class="btn btn-default" data-toggle="modal" data-target="#depShow" id="ck">查看</button>
                            </div>
                        </td>
                    </tr>`;
        }else {
            str1 = `<tr>
                        <td>${arr[i].classnum}</td>
                        <td>${arr[i].classname}</td>
                        <td>${arr[i].classtype}</td>
                        <td>
                            <div class="btn-group" role="group" aria-label="...">
                                <button type="button" class="btn btn-default" data-toggle="modal" data-target="#depDel" id="del1">删除</button>
                                <button type="button" class="btn btn-default" data-toggle="modal" data-target="#depUp" id="cup">修改</button>
                                <button type="button" class="btn btn-default" data-toggle="modal" data-target="#depShow" id="ck">查看</button>
                                <button type="button" class="btn btn-default" data-toggle="modal" data-target="#depTi" id="ti">提升</button>
                            </div>
                        </td>
                    </tr>`;
        }
        $(".dep_table").append(str1);
    }
}
//新增
function addDep(cname,ctype,ct1,ct2,arr){
    let timedate = new Date;
    let str = `${timedate.getFullYear()}-${timedate.getMonth()+1}-${timedate.getDate()}`;
    let num = parseInt(arr[arr.length-1].classnum)+1;
    if(num % 10 != 0) {
        num = "00"+ num;
    }else if(num % 100 != 0) {
        num += 1;
    }else {
        num = "00" + num;
    }
    var result = classtype.findIndex((v) => {
        return v.typename === ctype;
    });
    var add = {
        id: arr.length+1,
        classnum : `${num}`,
        classname : cname,
        classtype : ctype,
        typeid : classtype[result].id,
        teacher1 : ct1,
        teacher2 : ct2
    }
    console.log(add);
    arr.push(add);
    localStorage.setItem("classs",JSON.stringify(arr));
    classs = JSON.parse(localStorage.getItem("classs"));
    initia(arr);
    // location.reload();
}
//删除
function delDep() {

}
//修改
function upDep() {

}
//查看
function showDep() {

}
//搜索
function searDep(str) {
    let arr1 = [];
    classs = JSON.parse(localStorage.getItem("classs"));
    if(str != "") {
        var result = classs.findIndex((v) => {
            return v.classname === str;
        });
        if(result == -1) {
            $('#tis').popover('show');
            setTimeout(function(){
                $('#tis').popover('hide');
            },600)
        } else {
            arr1.push(classs[result]);
            initia(arr1);
        }
    }else {
        if(classs.length == $(".dep_table tbody").children().length-1) {
            $('#tis').popover('show');
            setTimeout(function(){
                $('#tis').popover('hide');
            },600)
        }else {
            location.reload();
        }
    }
}
function searDep1(str) {
    let arr1 = [];
    classs = JSON.parse(localStorage.getItem("classs"));
    if(str == "全部") {
        location.reload();
    }else {
        var result = classtype.findIndex((v) => {
            return v.typename === str;
        });
        for(let i = 0; i<classs.length; i++) {
            if(classs[i].typeid == classtype[result].id){
                arr1.push(classs[i]);
            }
        }
        initia(arr1);
    }
}
function fenye (arr) {
    let xia  = $(".xia").clone(true);
    $(".shang").nextAll().remove();
    $(".shang").after(xia);
    let num = 0;
    if(arr.length<=6) {
        pages = 1
        num = arr.length;
    }else {
        pages = parseInt(arr.length/6);
        num = 6;
        if(arr.length%6 != 0) {
            pages += 1;
            
        }
    }
    for (let i = 1; i <= pages; i++) {
        $(".xia").before(`<li class= ab,a${i} value = ${i}><a onclick = "dijifenye(this)">${i}</a></li>`);
    }
    if(page == 0) {
        $(".a1").addClass("active");
        page=1;
    }else {
        $(`.a${page}`).addClass("active");
        
    }
    if(page == 1) {
        $(".shang").addClass("disabled");
    }else {
        $(".shang").removeClass("disabled")
    }
    if(page == pages) {
        $(".xia").addClass("disabled");
    }else {
        $(".xia").removeClass("disabled")
    }
    let arr1 = arr.concat();

    return arr1.splice(page*6-6,num);
}
function dijifenye(num) {
    page = parseInt($(num).html());
    initia(fenyearr);
}





